package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ItemUser;
import cn.wolfcode.trip.base.query.ItemQueryObject;
import cn.wolfcode.trip.base.service.IItemService;
import cn.wolfcode.trip.base.service.IItemUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("items")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @Autowired
    private IItemUserService itemUserService;
    /**
     * 查询最新商品
     * @param qo
     * @return
     */
    @GetMapping("/push")
    public Object getAll(@ModelAttribute("qo") ItemQueryObject qo) {
        qo.setOrderBy("i.input_time desc"); // 时间降序排序
        qo.setPageSize(6);//只要前6个
        return itemService.query(qo);
    }

    /**
     * 根据商品分类id查询商品
     * @param catalogId
     * @return
     */
    @GetMapping("/{catalogId}")
    public Object getItemsByCatalogId(@PathVariable Long catalogId) {
        return itemService.getItemsByCatalogId(catalogId);
    }

    /**
     * 根据商品id查询商品信息
     * @param itemId
     * @return
     */
    @GetMapping("/{itemId}/info")
    public Object getItemsById(@PathVariable Long itemId) {
        return itemService.get(itemId);
    }

    /**
     * 获取用户购物车信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/shopcar")
    public Object getItemsByUserId(@PathVariable Long userId) {
        return itemUserService.selectByUserId(userId);
    }


    /**
     * 根据购物车记录查询要下单的商品信息
     * @param shopCarId
     * @return
     */
    @GetMapping("/confirm")
    public Object getConfirmInfo(Long shopCarId) {
        return itemUserService.getConfirmInfo(shopCarId);
    }

    /**
     * 修改购物车记录表中商品的数量
     * @param itemUser
     * @return
     */
    @PostMapping("/user")
    public JSONResult updateShopCarInfo(ItemUser itemUser) {
        JSONResult result = new JSONResult();
        try {
            itemUserService.updateShopCarInfo(itemUser);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
